package com.gc.business.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gc.business.domain.AdminRegion;
import com.gc.business.domain.GovRegion;
import com.gc.business.mapper.GovRegionMapper;
import com.gc.business.service.IGovRegionService;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gc.business.mapper.CompanyManageMapper;
import com.gc.business.domain.CompanyManage;
import com.gc.business.service.ICompanyManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

/**
 * 公司管理 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Service
public class CompanyManageServiceImpl extends ServiceImpl<CompanyManageMapper, CompanyManage> implements ICompanyManageService {

    @Autowired
    private GovRegionMapper govRegionMapper;

    @Override
    public List<CompanyManage> list(CompanyManage companyManage){
        LambdaQueryWrapper<CompanyManage> wrapper = new LambdaQueryWrapper<>();
        if(companyManage != null){
            if(StringUtils.isNotEmpty(companyManage.getId())){
                wrapper.eq(CompanyManage::getId, companyManage.getId());
            }
            if(StringUtils.isNotEmpty(companyManage.getCompanyId())){
                wrapper.eq(CompanyManage::getCompanyId, companyManage.getCompanyId());
            }
            if(StringUtils.isNotEmpty(companyManage.getRegionId())){
                wrapper.eq(CompanyManage::getRegionId, companyManage.getRegionId());
            }
            if(StringUtils.isNotEmpty(companyManage.getCompanyName())){
                wrapper.like(CompanyManage::getCompanyName, companyManage.getCompanyName());
            }
            if(StringUtils.isNotEmpty(companyManage.getCompanyLogo())){
                wrapper.eq(CompanyManage::getCompanyLogo, companyManage.getCompanyLogo());
            }
            if(StringUtils.isNotEmpty(companyManage.getLongitude())){
                wrapper.eq(CompanyManage::getLongitude, companyManage.getLongitude());
            }
            if(StringUtils.isNotEmpty(companyManage.getLatitude())){
                wrapper.eq(CompanyManage::getLatitude, companyManage.getLatitude());
            }
            if(StringUtils.isNotEmpty(companyManage.getCreateBy())){
                wrapper.eq(CompanyManage::getCreateBy, companyManage.getCreateBy());
            }
            if(StringUtils.isNotEmpty(companyManage.getCreateTime())){
                wrapper.eq(CompanyManage::getCreateTime, companyManage.getCreateTime());
            }
            if(StringUtils.isNotEmpty(companyManage.getUpdateBy())){
                wrapper.eq(CompanyManage::getUpdateBy, companyManage.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(companyManage.getUpdateTime())){
                wrapper.eq(CompanyManage::getUpdateTime, companyManage.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(companyManage.getRemark())){
                wrapper.eq(CompanyManage::getRemark, companyManage.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据政府机构ID查询区域下关联的公司列表
     * @param govId
     * @return
     */
    @Override
    public List<CompanyManage> selectCompanyManageByGovId(Long govId,Long regionId){
        Long[] regionIdArray = null;
        if(StringUtils.isNull(regionId)){
            //查询中间表配置的行政区域
            List<GovRegion> govRegionList = govRegionMapper.selectGovRegionByGovId(govId);
            String regionIds = "";
            for(int i=0;i<govRegionList.size();i++){
                regionIds += govRegionList.get(i).getRegionId() + ",";
                if(i == govRegionList.size() - 1){
                    regionIds += govRegionList.get(i).getRegionId();
                }
            }
            regionIdArray = Convert.toLongArray(regionIds);
        }else{
            regionIdArray = Convert.toLongArray(regionId.toString().concat(","));
        }
        return baseMapper.selectCompanyManageByRegionIds(regionIdArray);
    }


}
